package org.karelun.gef;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.karelun.gef.editors.DiagramEditorInput;
import org.karelun.gef.helper.ConstantMessages;
import org.karelun.gef.helper.IDConstant;
import org.karelun.gef.helper.ImageResource;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
	private IWorkbenchWindow window;
	private TrayItem trayItem;
	
	public ApplicationWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(1024, 768));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
		configurer.setTitle(ConstantMessages.app_Window_Title);
	}

	@Override
	public void postWindowOpen() {
		super.postWindowOpen();
		openEditor();
		window = getWindowConfigurer().getWindow();
		trayItem=initTaskItem(window);
		if(trayItem!=null){
			createMininize();
			hookPopupMenu(window);
		}
	}

	private void hookPopupMenu(final IWorkbenchWindow window) {
		trayItem.addListener(SWT.MenuDetect, new Listener(){
			public void handleEvent(Event event) {
				MenuManager trayMenu = new MenuManager();
				Menu menu = trayMenu.createContextMenu(window.getShell());
				fillTrayItemAction(trayMenu,window);
				menu.setVisible(true);
			}
		});
	}
	private void fillTrayItemAction(MenuManager trayMenu,
			IWorkbenchWindow window) {
		IAction exitAction=ActionFactory.QUIT.create(window);
		IAction aboutAction=ActionFactory.ABOUT.create(window);
		trayMenu.add(aboutAction);
		trayMenu.add(exitAction);
	}

	private void createMininize() {
		window.getShell().addShellListener(new ShellAdapter(){
			public void shellIconified(ShellEvent e){
				window.getShell().setVisible(false);
			}
		});
		trayItem.addListener(SWT.DefaultSelection, new Listener(){
			public void handleEvent(Event event) {
				Shell shell = window.getShell();
				if(!shell.isVisible()){
					shell.setVisible(true);
					window.getShell().setMinimized(false);
				}
			}
		});
	}

	private TrayItem initTaskItem(IWorkbenchWindow window) {
		final Tray tray = window.getShell().getDisplay().getSystemTray();
		TrayItem trayItem = new TrayItem(tray,SWT.NONE);
		ImageDescriptor imageDes = AbstractUIPlugin.imageDescriptorFromPlugin(
				Activator.PLUGIN_ID, ImageResource.CLASSMODEL);
		Image image = imageDes.createImage();
		trayItem.setImage(image);
		trayItem.setToolTipText("TrayItem"); //$NON-NLS-1$
		return trayItem;
	}

	private void openEditor() {
		IWorkbenchWindow window = getWindowConfigurer().getWindow();
		IPath path = Platform.getLocation();
		IEditorInput input=new DiagramEditorInput(path);
		IWorkbenchPage page = window.getActivePage();
		try {
			page.openEditor(input, IDConstant.ClassDiagramEditor_Id);
		} catch (PartInitException e) {

		}
	}
	public void dispose(){

	}
}
